<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<title>three.js examples</title>
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="./style/main.css">
	</head>
	<body>

		<div id="panel">

			<div id="header">

				<div id="sections">
					<span class="selected">examples</span>
				</div>

				<div id="expandButton"></div>
			</div>

			<div id="panelScrim"></div>

			<div id="contentWrapper">

				<div id="inputWrapper">
					<input placeholder="" type="text" id="filterInput" autocorrect="off" autocapitalize="off" spellcheck="false" />
					<div id="exitSearchButton"></div>
				</div>

				<div id="content">
					<img id="previewsToggler" src="./style/thumbnails.svg" width="20" height="20" />
				</div>
			</div>

		</div>

		<iframe id="viewer" name="viewer" allowfullscreen allowvr onmousewheel=""></iframe>

		<a id="button" target="_blank"><img src="./style/ic_code_black_24dp.svg"></a>

		<script src="./files.js"></script>

		<script>

		var panel = document.getElementById( 'panel' ); // 整个面板
		var content = document.getElementById( 'content' ); // 列表内容
		var viewer = document.getElementById( 'viewer' );	// iframe 内嵌内容
		var filterInput = document.getElementById( 'filterInput' ); // 过滤按钮
		var exitSearchButton = document.getElementById( 'exitSearchButton' );
		var expandButton = document.getElementById( 'expandButton' ); // 
		var viewSrcButton = document.getElementById( 'button' );
		var panelScrim = document.getElementById( 'panelScrim' );

		var previewsToggler = document.getElementById( 'previewsToggler' ); // 

		var links = {};
		var selected = null;
		var container = document.createElement( 'div' );

		init();

		function init() {

			content.appendChild( container );
			viewSrcButton.style.display = 'none';

			for ( var key in files ) {

				var section = files[ key ];

				var header = document.createElement( 'h2' );
				header.textContent = key;
				header.setAttribute( 'data-category', key );
				container.appendChild( header );

				for ( var i = 0; i < section.length; i ++ ) {

					var file = section[ i ];

					var link = createLink( file );
					container.appendChild( link );

					links[ file ] = link;

				}

			}

			if ( window.location.hash !== '' && links[ window.location.hash.substring( 1 ) ] ) {
				// console.log(window.location.hash.substring( 1 ))
				loadFile( window.location.hash.substring( 1 ) );

			}

			filterInput.value = extractQuery();

			if ( filterInput.value !== '' ) {

				panel.classList.add( 'searchFocused' );

			}

			updateFilter();

			// Events

			filterInput.onfocus = function ( ) {

				panel.classList.add( 'searchFocused' );

			};

			filterInput.onblur = function ( ) {

				if ( filterInput.value === '' ) {

					panel.classList.remove( 'searchFocused' );

				}

			};

			exitSearchButton.onclick = function ( ) {

				filterInput.value = '';
				updateFilter();
				panel.classList.remove( 'searchFocused' );

			};

			filterInput.addEventListener( 'input', function () {

				updateFilter();

			} );


			expandButton.addEventListener( 'click', function ( event ) {

				event.preventDefault();
				panel.classList.toggle( 'open' );

			} );

			panelScrim.onclick = function ( event ) {

				event.preventDefault();
				panel.classList.toggle( 'open' );

			};

			previewsToggler.onclick = function ( event ) {

				event.preventDefault();
				content.classList.toggle( 'minimal' );

			};

			// iOS iframe auto-resize workaround

			if ( /(iPad|iPhone|iPod)/g.test( navigator.userAgent ) ) {

				viewer.style.width = getComputedStyle( viewer ).width;
				viewer.style.height = getComputedStyle( viewer ).height;
				viewer.setAttribute( 'scrolling', 'no' );

			}

		}

		function createLink( file ) {


			var template = [
				'<div class="card">',
				'	<a href="demos/' + file + '/' + file + '.html" target="viewer">',
				'		<div class="cover">',
				'			<img src="screenshots/' + file  + '.jpg" loading="lazy" width="400" />',
				'		</div>',
				'		<div class="title">' + getName( file ) + '</div>',
				'	</a>',
				'</div>'
			].join( "\n" );

			var link = createElementFromHTML( template );

			link.querySelector( 'a[target="viewer"]' ).addEventListener( 'click', function ( event ) {
				if ( event.button !== 0 || event.ctrlKey || event.altKey || event.metaKey ) return;
				
				selectFile( file );
			} );
			return link;
		}

		function loadFile( file ) {

			selectFile( file );

			viewer.src = 'demos/' + file + '/' + file + '.html';

		}

		function selectFile( file ) {

			if ( selected !== null ) links[ selected ].classList.remove( 'selected' );
		
			links[ file ].classList.add( 'selected' );
			window.location.hash = 'demos/' + file + '/' + file;
			viewer.focus();
			panel.classList.remove( 'open' );

			selected = file;
			viewSrcButton.style.display = '';
			// viewSrcButton.href = 'http://gitlab.prod.dtstack.cn/visdev/easyexamples/tree/master/demos/' + selected + '/' + selected + '.html';
			// viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitLab';
			viewSrcButton.href = 'https://github.com/2912401452/2912401452.github.io/tree/master/easyvExamples/demos/' + selected + '/' + selected + '.html';
			viewSrcButton.title = 'View source code for ' + getName( selected ) + ' on GitHub';
		}

		function updateFilter() {

			var v = filterInput.value.trim();
			v = v.replace( /\s+/gi, ' ' ); // replace multiple whitespaces with a single one

			if ( v !== '' ) {

				window.history.replaceState( {}, '', '?q=' + v + window.location.hash );

			} else {

				window.history.replaceState( {}, '', window.location.pathname + window.location.hash );

			}

			var exp = new RegExp( v, 'gi' );

			for ( var key in files ) {

				var section = files[ key ];

				for ( var i = 0; i < section.length; i ++ ) {

					filterExample( section[ i ], exp );

				}

			}

			layoutList();

		}

		function filterExample( file, exp ) {

			var link = links[ file ];
			var name = getName( file );
			var res = file.match( exp );
			var text;

			if ( res && res.length > 0 ) {

				link.classList.remove( 'hidden' );

				for ( var i = 0; i < res.length; i ++ ) {

					text = name.replace( res[ i ], '<b>' + res[ i ] + '</b>' );

				}

				link.querySelector( '.title' ).innerHTML = text;

			} else {

				link.classList.add( 'hidden' );
				link.querySelector( '.title' ).innerHTML = name;

			}

		}

		function getName( file ) {

			var name = file.split( '_' );
			name.shift();
			return name.join( ' / ' );

		}

		function layoutList() {

			for ( var key in files ) {

				var collapsed = true;

				var section = files[ key ];

				for ( var i = 0; i < section.length; i ++ ) {

					var file = section[ i ];

					if ( links[ file ].classList.contains( 'hidden' ) === false ) {

						collapsed = false;
						break;

					}

				}

				var element = document.querySelector( 'h2[data-category="' + key + '"]' );

				if ( collapsed ) {

					element.classList.add( 'hidden' );

				} else {

					element.classList.remove( 'hidden' );

				}

			}

		}

		function extractQuery() {

			var p = window.location.search.indexOf( '?q=' );

			if ( p !== - 1 ) {

				return decodeURI( window.location.search.substr( 3 ) );

			}

			return '';

		}


		function createElementFromHTML( htmlString ) {

			var div = document.createElement( 'div' );
			div.innerHTML = htmlString.trim();
			return div.firstChild;

		}

		</script>

	</body>
</html>
